package Interface.Producao.Particular;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import sft.BancodeDados.CriaConexao;
import sft.Logica.Particular;

public class ParticularDao {
    private Connection conexao;
    
    public ParticularDao() throws SQLException {
        conexao = new CriaConexao().getConnection();
    }
    
    
    /*
     INSERT INTO sft.sft_particular (part_data_regist, part_os_ospart, part_vl_total, part_vl_desc, part_vl_liquido, part_vl_pago, part_vl_debito, part_alt_autorizado) 
     VALUES ('1', '1', 1.0, 11.0, 1.0, 1.0, 11.0, '1')
         
        
     Data | C�digo OS | Paciente | VL.Total | % Desc | VL. L�quido | VL. Pago | VL.Debito |  Autorizado por | 
     0          1         2           3           4         5           6          7               8
     */
    public void adicionar(List<Particular> p) throws SQLException{
        PreparedStatement stmt = conexao.prepareStatement(
                "INSERT INTO sft.sft_particular (part_data_regist, part_os_ospart, part_nm_paciente ,part_vl_total, part_vl_desc, part_vl_liquido, part_vl_pago, part_vl_debito, part_alt_autorizado) "
             +  "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
        
        
        for (int i = 0; i < p.size(); i++) {
            stmt.setString(1, p.get(i).getData());
            stmt.setString(2, p.get(i).getOs());
            stmt.setString(3, p.get(i).getPaciente());
            stmt.setDouble(4, p.get(i).getVlTotal());
            stmt.setDouble(5, p.get(i).getVlDesc());
            stmt.setDouble(6, p.get(i).getVlLiquido());
            stmt.setDouble(7, p.get(i).getVlPago());
            stmt.setDouble(8, p.get(i).getVlDebito());
            stmt.setString(9, p.get(i).getAutorizado());
            
            stmt.execute();
            stmt.clearParameters();
        }
        stmt.close();
    }
    
     public void clearTabela() throws SQLException{
        try (PreparedStatement stmt = conexao.prepareStatement("TRUNCATE TABLE sft.sft_particular")) {
            stmt.execute();
        }
    }
     
     public List<Particular> getParticular() throws SQLException{
        ResultSet rs;
        List<Particular> list;
        try (PreparedStatement stmt = conexao.prepareStatement("select * from sft_particular")) {
            rs = stmt.executeQuery();
            list = new ArrayList<>();
            while(rs.next()){
                Particular p = new Particular();
                p.setData(rs.getString("part_data_regist"));
                p.setOs(rs.getString("part_os_ospart"));
                p.setPaciente(rs.getString("part_nm_paciente"));
                p.setVlTotal(rs.getDouble("part_vl_total"));
                p.setVlDesc(rs.getDouble("part_vl_desc"));
                p.setVlLiquido(rs.getDouble("part_vl_liquido"));
                p.setVlPago(rs.getDouble("part_vl_pago"));
                p.setVlDebito(rs.getDouble("part_vl_debito"));
                p.setAutorizado(rs.getString("part_alt_autorizado"));
                list.add(p);
            }
        }
         rs.close();
         
         return list;
     }
     
     public List<Particular> getParticular(String os) throws SQLException{
        ResultSet rs;
        List<Particular> list;
        try (PreparedStatement stmt = conexao.prepareStatement("select * from sft_particular where sft_particular.part_os_ospart like ?")) {
            stmt.setString(1, "%" + os + "%");
            
            rs = stmt.executeQuery();
            list = new ArrayList<>();
            while(rs.next()){
                Particular p = new Particular();
                p.setData(rs.getString("part_data_regist"));
                p.setOs(rs.getString("part_os_ospart"));
                p.setPaciente(rs.getString("part_nm_paciente"));
                p.setVlTotal(rs.getDouble("part_vl_total"));
                p.setVlDesc(rs.getDouble("part_vl_desc"));
                p.setVlLiquido(rs.getDouble("part_vl_liquido"));
                p.setVlPago(rs.getDouble("part_vl_pago"));
                p.setVlDebito(rs.getDouble("part_vl_debito"));
                p.setAutorizado(rs.getString("part_alt_autorizado"));
                list.add(p);
            }
        }
         rs.close();
         
         return list;
     }
    
    
}
